package View.component 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.net.URLRequest;
	import flash.utils.setTimeout;
	import iscript.airFramework.util.FileHelper;
	import iscript.gameFramework.GameLite.events.GameContentEvent;
	import iscript.gameFramework.GameLite.Game;
	import iscript.gameFramework.GameLite.system.swfExplorer.events.SWFExplorerEvent;
	import iscript.gameFramework.GameLite.system.swfExplorer.SWFExplorer;
	import iscript.uiFramework.components.button.BaseButton;
	import iscript.uiFramework.components.container.ScrollBox;
	import iscript.uiFramework.components.form.ComboBox;
	import iscript.uiFramework.components.form.ListBox;
	import iscript.uiFramework.components.panel.FramePanel;
	import iscript.uiFramework.components.panel.Panel;
	import iscript.uiFramework.data.ComboBoxModel;
	import iscript.uiFramework.data.ListBoxModel;
	import iscript.uiFramework.event.ComboBoxEvent;
	import iscript.uiFramework.event.ListBoxEvent;
	import iscript.uiFramework.UIFramework;
	public class ExplorerMain extends Sprite
	{
		private var farmepanel:FramePanel;
		private var disc:ComboBox;
		private var fileList:ListBox;
		private var container:ScrollBox;
		private var _currentFolder:File;
		private var _classPanel:Panel;
		private var _classListBox:ListBox
		private var _currentFile:File;
		
		//private var _currentFilePath:String;
		
		public function ExplorerMain() 
		{
			  //farmepanel = new FramePanel(Game.graphicsDeviceManager.stage.stageWidth,Game.graphicsDeviceManager.stage.stageWidth
			  
			  var discComputer:Array = File.getRootDirectories();
			    var model:ComboBoxModel;
			  var cbbModel:Vector.<ComboBoxModel> = new Vector.<ComboBoxModel>();
			  for each (var item:File in discComputer) 
			  {
				      model = new ComboBoxModel()
				      model.text = item.name;
					  model.value = item;
					  model.bitmap = item.icon.bitmaps[1]
				  cbbModel.push(model);
			  }
			  
			   model = new ComboBoxModel()
			   model.text = File.desktopDirectory.name;
			   model.value = File.desktopDirectory
			   model.bitmap = File.desktopDirectory.icon.bitmaps[1]
			   cbbModel.push(model);
			  
			  
			   
			   
			  disc = new ComboBox()
			  disc.x = 10;
			  disc.y = 10;
			  disc.width = 200;
			  disc.setText("选择磁盘")
			  disc.storeData = cbbModel;
			  disc.addEventListener(ComboBoxEvent.CellRender_Select,selectComboBoxLogic)
			  disc.render(this);
			  
			  var folderUp:BaseButton = new BaseButton();
			  folderUp.x = 10;
			  folderUp.y = disc.y + disc.height + 3;
			  folderUp.height = 35;
			  folderUp.width = 200;
			  folderUp.text ="返回"
			  folderUp.icon = UIFramework.resource.getBitmapData("GameLite.UIComponent.DefaultTheme.Icon.FolderUp");
			  folderUp.ClickHandler = folderUpLogic;
			  folderUp.render(this);
			  
			  fileList = new ListBox();
			  fileList.border = true;
			  fileList.getBorder().color = 0x99BBE8;
			  fileList.x = 10;
			  fileList.y = folderUp.y + folderUp.height + 3;
			  fileList.height = 600
			  fileList.render(this);
			  fileList.addEventListener(ListBoxEvent.CellRender_Select, selectListBoxLogic)
			  
			  
			  container = new ScrollBox(700, fileList.y+600-10);
			  container.x = 220;
			  container.y = 10;
			  container.border = true;
			  container.render(this);
			  
			  _classPanel = new Panel(300, fileList.y + 600 - 10);
			  _classPanel.title = "类名" 
			 _classPanel.x = container.x + container.width + 10;
			 _classPanel.y = 10;
			  _classPanel.render(this)
			  
			  
			  _classListBox = new ListBox(_classPanel.body.width, _classPanel.body.height)
			  _classListBox.addEventListener(ListBoxEvent.CellRender_Select,classSelectLogic)
			  _classPanel.body.append(_classListBox)
			  //trace(_classPanel.body.width, _classPanel.body.height,"==============");
			  setFileList(File(model.value))
			  
		}
		
		private function classSelectLogic(e:ListBoxEvent):void 
		{
			container.clear();
			var data:ListBoxModel  =  e.selectData;
			 
			var cls:Class = Game.Content.getSWF(_currentFile.nativePath).getClass(data.text)
			var targetShow:* = new cls();
			
			if(targetShow is BitmapData)
			 targetShow =new Bitmap(targetShow)
			
			targetShow.x= container.width/2- DisplayObject(targetShow).width/2
			targetShow.y= container.height/2-DisplayObject(targetShow).height/2
			container.append(targetShow)
			
			
		}
		
		
		private function  folderUpLogic(e:*):void {
			 if(_currentFolder!=null && _currentFolder.parent !=null)
			  setFileList(_currentFolder.parent);
			}
		
			
			
		/**
		 * 选择PNG Or SWF 
		 * @param	e
		 */
		private function selectListBoxLogic(e:ListBoxEvent):void 
		{

			if (!File(e.selectData.value).isDirectory) {
				_currentFile = File(e.selectData.value);
				  if (FileHelper.IsFile(_currentFile, ".swf")) {
					  
					  var swfExplorer:SWFExplorer = new SWFExplorer();
					  Game.Content.addSwf(_currentFile.nativePath);
					  swfExplorer.load(new URLRequest(_currentFile.nativePath));
					  swfExplorer.addEventListener(SWFExplorerEvent.COMPLETE, assetsReady);
					  }
				  if (FileHelper.IsFile(_currentFile, ".png")) {
					      _classListBox.storeData = null;
					      container.clear();
						  Game.Content.addGameContentListener(_currentFile.nativePath, renderPngImageLogic )
					      Game.Content.addImage(_currentFile.nativePath)
					  }	  
				}else {			
					 _currentFolder = File(e.selectData.value);
					setFileList(_currentFolder)
				}
		}
		
		private function renderPngImageLogic(e:GameContentEvent):void 
		{
			container.append(Game.Content.getImage(_currentFile.nativePath).content);
			//trace(e.content);
		}
		 
		
		private   function assetsReady (e:SWFExplorerEvent):void
		{
		//trace( e.definitions );
		var listModel:Vector.<ListBoxModel> = new Vector.<ListBoxModel>();
		 for each (var cls:String in  e.definitions) {
				     var folderModel:ListBoxModel = new ListBoxModel();
					     folderModel.text = cls;
						 folderModel.value = cls;
						 //folderModel.bitmap = folder.icon.bitmaps[1]
						 //UIFramework.resource.getBitmapData("GameLite.UIComponent.DefaultTheme.Icon.FolderClosed")
					 listModel.push(folderModel)
				  }
		_classListBox.storeData = listModel;

		}
		
		
		private function selectComboBoxLogic(e:ComboBoxEvent):void 
		{
			setFileList(File(e.selectData.value))
		}
		
		
		private function  setFileList(currentfile:File):void {
			
			 if (!currentfile.isDirectory) return;
			 _currentFolder = currentfile;
			 trace(_currentFolder.nativePath)
			  var listModel:Vector.<ListBoxModel> = new Vector.<ListBoxModel>();
			  for each (var folder:File in FileHelper.getFolderList(currentfile)) {
				     var folderModel:ListBoxModel = new ListBoxModel();
					     folderModel.text = folder.name;
						 folderModel.value = folder;
						 folderModel.bitmap = folder.icon.bitmaps[1]
						 //UIFramework.resource.getBitmapData("GameLite.UIComponent.DefaultTheme.Icon.FolderClosed")
					 listModel.push(folderModel)
				  }
			       
		 	   for each (var file:File in FileHelper.getFiles(currentfile,".png,.swf")) {
				    var fileModel:ListBoxModel = new ListBoxModel();
					     fileModel.text = file.name;
						 fileModel.value = file;
						 fileModel.bitmap =  file.icon.bitmaps[1] 
						 //UIFramework.resource.getBitmapData("GameLite.UIComponent.DefaultTheme.Icon.File")
					 listModel.push(fileModel)
			   }
				  
				  fileList.storeData = listModel; 
			}
 
		 
		
		
		
		
		
		
		
		
	}

}





 